(function($, window) {

	//网络状态更新监听器
	(function() {
		let onNetworkStatusListener = null;

		/**
		 * 网络状态被改变
		 * @param {{type:String}} res
		 */
		function callbackNetworkStatusListener(res) {
			onNetworkStatusListener && onNetworkStatusListener.call(null, res);
		}

		window.addEventListener('online', callbackNetworkStatusListener);
		window.addEventListener('offline', callbackNetworkStatusListener);

		/**
		 * 设置网络状态监听
		 * @param {Function<{type:String}>} callback
		 */
		$.define('setOnNetworkStatusListener', function(callback) {
			onNetworkStatusListener = callback;
		});
	})();


	/**
	 * 获取当前系统信息
	 * @returns {{appName: string, appVersion: string, platform: string, mimeTypes: MimeTypeArray,
	 * language: string, languages: string[], width: number, height: number, colorDepth: number,
	 * deviceXDPI: number, fontSmoothingEnabled: boolean}}
	 */
	$.define('getSystemInfo', function() {
		const userAgents = navigator.userAgent.split(' ');
		const app = userAgents[userAgents.length - 1].split('/'),
			appName = app.length === 2 ? app[0] : navigator.appCodeName,
			appVersion = app.length === 2 ? app[1] : '0.0.0';

		return {
			appName: appName,
			appVersion: appVersion,
			platform: navigator.platform,
			mimeTypes: navigator.mimeTypes,
			language: navigator.language,
			languages: navigator.languages,
			width: window.screen.width,
			height: window.screen.height,
			colorDepth: window.screen.colorDepth,
			deviceXDPI: window.screen.deviceXDPI,
			fontSmoothingEnabled: window.screen.fontSmoothingEnabled,
		};
	});

	/**
	 * 获取网络类型
	 * @returns {string}
	 */
	$.define('getNetworkType', function() {
		return "unknown";
	});

	/**
	 * 获取网络状态
	 * @returns {string}
	 */
	$.define('getNetworkStatus', function() {
		return navigator.onLine ? "online" : "offline";
	});

	/**
	 * 当前网络状态是否在线
	 * @returns {boolean}
	 */
	$.define('isNetworkOnline', function() {
		return navigator.onLine;
	});

	/**
	 * 当前网络状态是否下线
	 * @returns {boolean}
	 */
	$.define('isNetworkOffline', function() {
		return !navigator.onLine;
	});

	/**
	 * 设置导航标题
	 * @param {String} title
	 */
	$.define('setNavigateTitle', function(title) {
		window.document.title = title;
	});

	/**
	 * 获取当前窗口
	 * @return {{webContents: {isDevToolsOpened: function(): boolean}}}
	 */
	$.define('getCurrentWindow', function() {
		const defaultWindow = {
			webContents: {
				isDevToolsOpened: () => false
			}
		};
		return window.require ? window.require('electron').remote.getCurrentWindow() : defaultWindow;
	});
})($, typeof window === "undefined" ? {
	addEventListener() {
	}
} : window);
